/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;

import com.lineage.configure.ConfigureServer;
import com.lineage.server.utils.SystemUtil;

/**
 * Class <code>GameServer</code> 游戏服务器.
 * 
 * @author jrwz
 * @version 2012-5-24上午12:33:55
 * @see com.lineage.server
 * @since JDK1.7
 */
public final class GameServer extends Thread {

    /** 提示信息. */
    private static final Logger LOG = Logger.getLogger(GameServer.class
            .getName());
    /** 实例. */
    private static GameServer _instance;

    /**
     * 取得实例（第一次实例为 null 的时候才新建立一个).
     * 
     * @return 实例
     */
    public static GameServer getInstance() {
        if (_instance == null) {
            _instance = new GameServer();
        }
        return _instance;
    }

    /** 服务器端口. */
    private int _port;

    /** 服务器套接字等待请求通过网络传入. */
    private ServerSocket _serverSocket;

    /** 游戏服务器. */
    private GameServer() {
        super("GameServer");
    }

    /**
     * 初始化.
     * 
     * @throws Exception
     *             构造详细消息为 null 的新异常。
     *             原因尚未被初始化，
     *             可在以后通过调用 Throwable.initCause(java.lang.Throwable) 对其进行初始化。
     */
    public void initialize() throws Exception {
        final String host = ConfigureServer.GAME_SERVER_HOST_NAME; // 服务器IP
        this._port = ConfigureServer.GAME_SERVER_PORT; // 服务器端口
        if (!"*".equals(host)) {
            final InetAddress inetaddress = InetAddress.getByName(host);
            inetaddress.getHostAddress();
            this._serverSocket = new ServerSocket(this._port, 50, inetaddress);
            System.out.println("服务器端口: " + this._port);
        } else {
            this._serverSocket = new ServerSocket(this._port);
            System.out.println("服务器端口: " + this._port);
        }
        final String msg = "\n\r-----------------------------------------------"
                + "\n\r        \t\t服务器启动"
                + "\n\r-----------------------------------------------";
        LOG.info(msg);
        this.start();
    }

    @Override
    public void run() {
        System.out.println("消耗内存: " + SystemUtil.getUsedMemoryMB() + " MB");
        System.out.println("等待客户端连接...");
        while (true) {
            try {
                // 侦听并接受到此套接字的连接。
                final Socket socket = this._serverSocket.accept();
                System.out.println("尝试连线的IP: " + socket.getInetAddress());

            } catch (final IOException ioexception) {
            }
        }
    }
}
